package com.bytedance.danmaku.render.engine.demo.danmu.bordertext

import android.graphics.Canvas
import android.util.Log
import com.bytedance.danmaku.render.engine.control.DanmakuConfig
import com.bytedance.danmaku.render.engine.data.DanmakuData
import com.bytedance.danmaku.render.engine.demo.demo.advanced.AdvancedDanmakuData
import com.bytedance.danmaku.render.engine.demo.demo.advanced.AdvancedDanmakuDrawItem
import com.bytedance.danmaku.render.engine.demo.demo.advanced.DiggData
import com.bytedance.danmaku.render.engine.demo.demo.advanced.DiggDrawItem
import com.bytedance.danmaku.render.engine.demo.utils.DRAW_TYPE_ADVANCED
import com.bytedance.danmaku.render.engine.demo.utils.DRAW_TYPE_SIMPLE_ADVANCED
import com.bytedance.danmaku.render.engine.demo.utils.dp
import com.bytedance.danmaku.render.engine.render.draw.DrawItem
import com.bytedance.danmaku.render.engine.render.draw.IDrawItemFactory
import com.bytedance.danmaku.render.engine.render.draw.text.TextData
import com.bytedance.danmaku.render.engine.render.draw.text.TextDrawItem

/**
 * @Description:自定义简单高级弹幕
 * @Author yanxin
 * @Date:  2025/8/20
 */

class SimpleAdvancedDanmaku : DanmakuData() {
    var textData: TextData? = null
    var diggData: DiggData? = null   //基本的图文混排
    var diggIsRight: Boolean = true  //控制图片是否在文字右边
    override var drawType: Int = DRAW_TYPE_SIMPLE_ADVANCED
}

class SimpleAdvancedDanmakuDrawItem : DrawItem<SimpleAdvancedDanmaku>() {
    override var x: Float = 0F
        set(value) {
            field = value
            updateXY()
        }
    override var y: Float = 0F
        set(value) {
            field = value
            updateXY()
        }
    private val mTextItem: TextDrawItem = TextDrawItem()
    private val mDiggItem: DiggDrawItem = DiggDrawItem()
    private var diggIsRight: Boolean = true
    private val mDiggMargin: Float = 8.dp
    private var mHasDigg = false
    override fun getDrawType(): Int {
        return DRAW_TYPE_SIMPLE_ADVANCED
    }

    override fun onBindData(data: SimpleAdvancedDanmaku) {
        mHasDigg = data.diggData != null
        data.diggIsRight.let {
            diggIsRight = it
        }
        data.textData?.let {
            mTextItem.bindData(it)
        }
        data.diggData?.let {
            mDiggItem.bindData(it)
        }
    }

    override fun onMeasure(config: DanmakuConfig) {
        mTextItem.measure(config)
        mDiggItem.measure(config)
        height = mTextItem.height
        width = mTextItem.width
        mHasDigg = data?.diggData != null
        if (mHasDigg) {
            width += mDiggMargin + mDiggItem.width
        }
        updateXY()
    }

    override fun onDraw(canvas: Canvas, config: DanmakuConfig) {
        mTextItem.draw(canvas, config)
        if (mHasDigg) {
            mDiggItem.draw(canvas, config)
        }
    }

    override fun recycle() {
        super.recycle()
        mTextItem.recycle()
        mDiggItem.recycle()
    }

    private fun updateXY() {
        if (diggIsRight) {
            mTextItem.x = x
            mTextItem.y = y
            // Place diggItem to in the end of the text with margin
            mDiggItem.x = mTextItem.x + mTextItem.width + mDiggMargin
            // Place digg icon center vertically
            mDiggItem.y = y + (height - mDiggItem.height) / 2
        } else {
            mDiggItem.x = x
            // Place digg icon center vertically
            mDiggItem.y = y + (height - mDiggItem.height) / 2

            mTextItem.x = mDiggItem.x + mDiggItem.width + mDiggMargin
            mTextItem.y = y + (height - mTextItem.height) / 2
        }
    }

    class SimpleAdvancedDanmakuFactory : IDrawItemFactory {
        override fun getDrawType(): Int {
            return DRAW_TYPE_SIMPLE_ADVANCED
        }

        override fun generateDrawItem(): DrawItem<out DanmakuData> {
            Log.e("DanmakuFactory", "generateDrawItem")
            return SimpleAdvancedDanmakuDrawItem()
        }
    }
}

